//
//  LXJEmoticonKeyBoard.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/4/7.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

/// 自定义键盘视图

/*
 视图组成:
 1. 表情区域
 2. 分页控制器
 3. 工具条
 */
@available(iOS 9.0, *)
class LXJEmoticonKeyBoard: UIView {
    
    // MARK: 控件的懒加载
    /// 工具条
    fileprivate lazy var toolBar: LXJEmoticonKeyBoardToolBar = LXJEmoticonKeyBoardToolBar()
    
    /// 表情键盘
    fileprivate lazy var emoticonKeyBoard: LXJEmoticonKeyBoardEmoticon = {
        /// 获取布局对象
        let flowLayout = UICollectionViewFlowLayout()
        /// 设置水平方向滚动
        flowLayout.scrollDirection = .horizontal
        /// 创建表情键盘
        let collectionView = LXJEmoticonKeyBoardEmoticon(frame: CGRect.zero, collectionViewLayout: flowLayout)
        /// 开启分页效果
        collectionView.isPagingEnabled = true
        /// 关闭水平垂直方向的指示器
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        /// 关闭弹簧效果
        collectionView.bounces = false
        return collectionView
    }()
    
    /// 分页指示器
    fileprivate lazy var pageControl: UIPageControl = {
        let page = UIPageControl()
        
        /// 设置对应的图片
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        /// 关闭用户交互
        page.isUserInteractionEnabled = false
        /// 单行隐藏
        page.hidesForSinglePage = true
        return page
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        /// 界面加载
        setupUI()
    }
    
    /// 不使用 XIB 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 页面搭建
    fileprivate func setupUI() -> Void {
        /// 设置背景颜色
        backgroundColor = UIColor.white
        
        /// 系统默认滚动到默认表情这组数据
        let normalIndexPath = IndexPath(item: 0, section: 1)
        DispatchQueue.main.async {
            /// 让 表情区滑动至目标区域
            self.emoticonKeyBoard.scrollToItem(at: normalIndexPath, at: [], animated: true)
            /// 滑动的时候跟新怕个 control 的页数
            self.setPageControlData(indexPath: normalIndexPath)
        }
        
        /// 添加子控件
        addSubview(toolBar)
        addSubview(emoticonKeyBoard)
        addSubview(pageControl)
        
        /// 自动布局
        toolBar.snp.makeConstraints { (make) in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        emoticonKeyBoard.snp.makeConstraints { (make) in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.bottom.leading.trailing.equalTo(emoticonKeyBoard)
            make.height.equalTo(8)
        }
        
        /// 执行点击摁扭时候的闭包
        toolBar.buttonClickClosure = {[weak self] (type: EmoticonKeyBoardToolBarButtonType) -> () in
            /// 获取下标
            let indexPath: IndexPath
            
            switch type {
            case .recent:
                indexPath = IndexPath(item: 0, section: 0)

            case .normal:
                indexPath = IndexPath(item: 0, section: 1)
                
            case .emoji:
                indexPath = IndexPath(item: 0, section: 2)
                
            case .langxiaohua:
                indexPath = IndexPath(item: 0, section: 3)
            }
            
            /// 滚动到对应的下标
            self?.emoticonKeyBoard.scrollToItem(at: indexPath, at: [], animated: true)
        }
        
        /// 滑动的时候执行滑动闭包
        emoticonKeyBoard.scrollClosure = {[weak self] (indexPath: IndexPath) -> () in
            /// 根据索引选中对应的摁扭
            self?.toolBar.selectedButton(indexPath: indexPath)
            self?.setPageControlData(indexPath: indexPath)
        }
    }
    
    /// 调整子控件布局
    override func layoutSubviews() {
        /// 获取布局对象
        let flowLayout = emoticonKeyBoard.collectionViewLayout as! UICollectionViewFlowLayout
        /// 这只每个单元格的大小
        flowLayout.itemSize = emoticonKeyBoard.size
        /// 设置间距
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
    }
    
    /// 根据index绑定 pageCOntrol 的数据
    fileprivate func setPageControlData(indexPath: IndexPath) -> Void {
        pageControl.numberOfPages = LXJEmoticonToolManager.sharedManager.allEmoticonArray[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
}
